import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
const DB = require('../../../app/db');
import update from 'react-addons-update';
import Utils from './../../../utils'
import StorageUtils from './../../../app/storage'
import nattyStorage from 'natty-storage';

export default Reflux.createStore({
    listenables: [Actions],
    data: {
        ...PageConst,
      lastPage: false,
      keyword: '',
      currentPage: 1,
      pageSize: 3,
      jdfzr: '',
      engls:[],
      userInfo: {
        certNum: '',
        telNum: '',
        vcode: '',
        name:'',
        organName:''
      },
    },

    onFetch: function (params, cb) {
        let t = this;
        t.updateComponent();
        cb && cb(t.data);
    },

  onEngInit: function () {
    let t = this;
    this.data.userInfo = update(this.data.userInfo, {$merge: {...StorageUtils.getStorage().get('user')}});
    if (t.data.engls.length == 0) {
      this.onEngFetch({
        page: 1,
        jdfzr:'',
        size: 3,
      });
    }
    t.updateComponent();
  },

  onEngFetch: function (params) {

    let t = this;
    console.log('engfetch');
    params.page = t.data.currentPage;
    params.size = t.data.pageSize;
    params.jdfzr = this.data.userInfo.telNum;
    DB.WechatAPI.getEng(params).then(
      function (content) {
        if (content.length <= 0) {
        }
        else
        {
          t.data.engls = update(t.data.engls, {$set: content} );
          t.updateComponent();
        }
        t.data.lastPage = true;
      },
      function (error) {
        console.log(error);
      });
  },

  updateComponent: function () {
        this.trigger(this.data);
    },

  getInitialState: function () {
        return this.data;
    }
});
